--Open this script in a new Script Editor window.

(*
v 2.0.1b, 12 Feb 2004, ccons at ccons dot net

As-nBs to iCal.scpt is an AppleScript that exports contact anniversaries and birthdays from Apple Mac OS X Address Book to iCal.

It adds a new calendar to iCal and will not overwrite existing events. It creates the contact's full name using the nickname in lieu of the first name, if available. To avoid duplicate anniversary events, a contact's spouse name must use the spouse's contact's nickname, if available.

Simply run from the desktop or install in "~/Library/Scripts/Address Book Scripts" or "/Library/Scripts/Address Book Scripts" and run from Address Book's AppleScript menu.

This has only been used with Mac OS X 10.3.2, Address Book 3.1.1 (v301), iCal 1.5.2 (v637), Script Editor 2.0 (v43), and AppleScript 1.9.3. Your mileage with different versions may vary.

This software is provided free of charge and no warrantee is expressed or implied. Use at your own risk.
*)

set debug to false
set theExportCalendarTitleString to "As-n-Bs"
set theFinishedDialogstring to "Finished exporting items."

tell application "iCal" to set theCalendar to make new calendar with properties {title:theExportCalendarTitleString}

tell application "Address Book" to set everyPerson to every person

repeat with eachPerson in everyPerson
    using terms from application "Address Book"
        set theBirthDate to eachPerson's birth date
        tell eachPerson to set thecustomdates to (every custom date whose label is equal to "anniversary")
    end using terms from
    
    if (theBirthDate is not missing value) or (thecustomdates is not equal to {}) then
        using terms from application "Address Book"
            set theNickname to eachPerson's nickname
            set theFirstName to eachPerson's first name
            set theLastname to eachPerson's last name
        end using terms from
        
        if theNickname is not missing value then
            set theDisplayFirstName to theNickname
        else
            set theDisplayFirstName to theFirstName as string
        end if
        set theDisplayName to theDisplayFirstName & " " & theLastname
    end if
    
    if theBirthDate is not missing value then
        set theBirthYearstring to (year of the (theBirthDate)) as string
        set theSummary to theDisplayName & ", Born " & theBirthYearstring
        
        if debug is true then
            display dialog theSummary buttons {"OK", "Cancel"}
        end if
        tell application "iCal" to make new event at the end of theCalendar with properties {summary:theSummary, start date:theBirthDate, allday event:true, recurrence:"FREQ=YEARLY"}
    end if
    
    if thecustomdates is not equal to {} then
        set theAnniversarycustomdate to item 1 of thecustomdates
        using terms from application "Address Book"
            tell eachPerson
                set theAnniversarydate to value of theAnniversarycustomdate
                set theSpouseName to (the value of item 1 of (every related name whose label is equal to "spouse")) as string
            end tell
        end using terms from
        
        set theAnniversaryYearstring to (year of theAnniversarydate) as string
        set theEventExists to false
        if theSpouseName is not missing value then
            set theSummary to theDisplayName & " & " & theSpouseName & ", Married " & theAnniversaryYearstring
            set theExistingSummary to theSpouseName & " & " & theDisplayName & ", Married " & theAnniversaryYearstring
            using terms from application "iCal"
                tell theCalendar
                    if (the count of (every event whose summary is equal to theExistingSummary)) is greater than 0 then
                        set theEventExists to true
                    end if
                end tell
            end using terms from
        else
            set theSummary to theDisplayName & ", Married " & theAnniversaryYearstring
        end if
        
        if debug is true then
            display dialog theSummary as string
        end if
        if theEventExists is false then
            tell application "iCal" to make new event at the end of theCalendar with properties {summary:theSummary, start date:theAnniversarydate, allday event:true, recurrence:"FREQ=YEARLY"}
        end if
    end if
end repeat

display dialog theFinishedDialogstring buttons {"OK"}